﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pyramid.VsOnline.Core.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pyramid.VsOnline.Core.Model;
namespace Pyramid.VsOnline.Core.Services.Tests
{
    [TestClass()]
    public class TfsParamParserTests
    {
        [TestMethod()]
        public void ParseWorkfoldParametersTest()
        {
            var arguments = new string[]{
                "workfold",
                "-map",
                @"$/PyramidVsOnlineTF",
                @"C:\Users\irabantac\.hudson\jobs\PyramidVsOnlineTF\workspace",
                "-workspace:Hudson-PyramidVsOnlineTF",
                "-noprompt",
                "-server:https://tfs.codeplex.com/TFS31",
                "-login:testlogin,testpassword"
            };
            var tfsParamParser = new TfsParamParser();
            var tfsParameters = tfsParamParser.Parse(arguments);
            Assert.AreEqual(tfsParameters.Silent, true, "Silent not correct");
            Assert.AreEqual(tfsParameters.ServerUrl, "https://tfs.codeplex.com/TFS31", "ServerUrl not correct");
            Assert.IsNotNull(tfsParameters.Credentials, "Credentials not found");
            Assert.AreEqual(tfsParameters.IsWorkfold, true, "IsWorkfold not correct");
            Assert.AreEqual(tfsParameters.Workfold.ServerProjectPath, "$/PyramidVsOnlineTF", "serverpath not correct");
            Assert.AreEqual(tfsParameters.Workfold.TargetPath, @"C:\Users\irabantac\.hudson\jobs\PyramidVsOnlineTF\workspace", "workspace not correct");
            Assert.AreEqual(tfsParameters.Workfold.Workspace, "Hudson-PyramidVsOnlineTF", "Workspace not correct");
        }

        [TestMethod()]
        public void ParseHistoryParametersTest()
        {
            //@"history|$/pci/Trunk|-noprompt|-version:D2014-07-01T04:38:40Z~D2014-07-01T07:01:00Z|-recursive|-format:detailed
            var arguments = new string[]{
                "history",
                @"$/PyramidVsOnlineTF",
                "-noprompt",
                @"-version:D2014-07-01T04:38:40Z~D2014-07-01T07:01:00Z",
                "-recursive",
                "-format:detailed",
                "-server:https://tfs.codeplex.com/TFS31",
                "-login:testlogin,testpassword"
            };
            var tfsParamParser = new TfsParamParser();
            var tfsParameters = tfsParamParser.Parse(arguments);
            Assert.AreEqual(tfsParameters.ServerUrl, "https://tfs.codeplex.com/TFS31", "ServerUrl not correct");
            Assert.AreEqual(tfsParameters.IsGetHistory, true, "IsGetHistory not correct");
            Assert.AreEqual(tfsParameters.History.ServerProjectPath, "$/PyramidVsOnlineTF", "serverpath not correct");
            Assert.IsNotNull(tfsParameters.History.VersionFrom, "VersionFrom not correct");
            Assert.IsNotNull(tfsParameters.History.VersionTo, "VersionTo not correct");
            Assert.AreEqual(tfsParameters.History.IsRecursive, true, "Reucrsive not correct");
            Assert.AreEqual(tfsParameters.History.Format, WorkspaceFormatType.Detailed, "Format not correct");
            Assert.AreEqual(tfsParameters.Silent, true, "Silent not correct");
            Assert.IsNotNull(tfsParameters.Credentials, "Credentials not found");
        }

        [TestMethod()]
        public void ParseGetLatestVersionParametersTest()
        {
            var args=@"get|C:\Users\irabantac\.hudson\jobs\Pyramid MVC5 Template\workspace|-recursive|-version:D2014-08-24T11:57:26Z|-noprompt|-login:testloginName,password";
            var arguments = args.Split(new char[] { '|' });
            var tfsParamParser = new TfsParamParser();
            var tfsParameters = tfsParamParser.Parse(arguments);
            Assert.AreEqual(tfsParameters.IsGetLatestVersion, true, "IsGetLatestVersion not correct");
            Assert.AreEqual(tfsParameters.LatestVersion.TargetPath, @"C:\Users\irabantac\.hudson\jobs\Pyramid MVC5 Template\workspace", "TargetPath not correct");
            Assert.IsNotNull(tfsParameters.LatestVersion.VersionFrom, "VersionFrom not correct");
            //Assert.IsNotNull(tfsParameters.LatestVersion.VersionTo, "VersionTo not correct");
            Assert.AreEqual(tfsParameters.LatestVersion.IsRecursive, true, "Reucrsive not correct");
            Assert.AreEqual(tfsParameters.Silent, true, "Silent not correct");
            //Assert.IsNotNull(tfsParameters.Credentials, "Credentials not found");
        }


        [TestMethod()]
        public void ParseWorkspacesParametersTest()
        {
            //@"workspaces -format:brief -server:https://apg-alogent.visualstudio.com/DefaultCollection"
            var arguments = new string[]{
                "workspaces",
                "-format:brief",
                "-server:https://tfs.codeplex.com/TFS31",
                "-login:testlogin,testpassword"
            };
            var tfsParamParser = new TfsParamParser();
            var tfsParameters = tfsParamParser.Parse(arguments);
            Assert.AreEqual(tfsParameters.IsWorkspaces, true, "IsWorkspaces not correct");
            Assert.AreEqual(tfsParameters.ServerUrl, "https://tfs.codeplex.com/TFS31", "ServerUrl not correct");
            Assert.AreEqual(tfsParameters.Workspaces.Format, WorkspaceFormatType.Brief, "Format not correct");
            Assert.IsNotNull(tfsParameters.Credentials, "Credentials not found");
        }

        [TestMethod()]
        public void ParseNewWorkspaceParametersTest()
        {
            //@"workspace -new "Hudson-Alogent Aware CI-MASTER;chingangbam.irabanta@pyramidci.com" -noprompt -server:https://apg-alogent.visualstudio.com/DefaultCollection
            var arguments = new string[]{
                "workspace",
                "-new",
                "Hudson-PyramidVsOnlineTF;testlogin",
                "-noprompt",
                "-server:https://tfs.codeplex.com/TFS31",
                "-login:testlogin,testpassword"
            };
            var tfsParamParser = new TfsParamParser();
            var tfsParameters = tfsParamParser.Parse(arguments);
            Assert.AreEqual(tfsParameters.IsCreateWorkspace, true, "IsCreateWorkspace not correct");
            Assert.AreEqual(tfsParameters.ServerUrl, "https://tfs.codeplex.com/TFS31", "ServerUrl not correct");
            Assert.IsNotNull(tfsParameters.Credentials, "Credentials not found");
            Assert.AreEqual(tfsParameters.Silent, true, "Silent not correct");
            Assert.AreEqual(tfsParameters.WorkspaceCreate.WorkspaceName, "Hudson-PyramidVsOnlineTF", "Workspace name not correct");
            Assert.AreEqual(tfsParameters.WorkspaceCreate.Owner, "testlogin", "Workspace owner not correct");
        }
    }
}
